package com.pianzhu.blockchainemr;

import com.pianzhu.blockchainemr.utils.AliyunOSSOperator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

@SpringBootTest
class BlockChainEmrApplicationTests {

    @Test
    void contextLoads() {
    }

    @Autowired
    private AliyunOSSOperator ossOperator;
    private String url = "https://pianzhu-my-app-learn.oss-cn-beijing.aliyuncs.com/2025/10/aed7da37-4a79-4a35-96e8-bbad0b4ee4fa.txt";
    @Test
    void OSSTest() {
        File file = new File("D:\\MyProjects\\Java\\Maven\\BlockChainEMR\\src\\test\\java\\com\\pianzhu\\blockchainemr\\img.png");
        byte[] b = new byte[1024];
        try {
            FileInputStream fis = new FileInputStream(file);

            //新的 byte 数组输出流，缓冲区容量1024byte
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            //改变为byte[]
            byte[] data = bos.toByteArray();
            //
            bos.close();
            url = ossOperator.upload(data, file.getName());
            System.out.println(url);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
